<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Gambar
 *
 * @author BAGUS
 */
include '../database/koneksi/Koneksi.php';

class Gambar {

    //put your code here
    var $id_gambar; //tidak boleh kosong
    var $url_gambar_asli;
    var $url_gambar_tumbnail;
    var $id_barang; //tidak boleh kosong

    public function getid_gambar() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select * from gambar where id_barang = '"
                . mysql_real_escape_string($this->getid_barang()) . "' order by id_gambar");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($result)) {
            $id_gambar1 = $row["id_gambar"];
        }
        if ($id_gambar1 == '') {
            $id_gambar1 = $this->getid_barang() . '01';
            $this->id_gambar = $id_gambar1;
        } else {
            $id_barang1 = substr($id_gambar1, 0, -2);
            $id_barang = $this->getid_barang();
            if ($$id_barang == $id_barang1) {
                $id_Gambarr = intval($id_barang1) + 1;
                $this->id_Barang = intval($id_Gambarr);
            } else {
                $id_gambar1 = $this->getid_barang() . '01';
                $this->id_gambar = $id_gambar1;
            }
        }
        return $this->id_gambar;
    }

    public function setid_barang($id_barang) {
        $this->id_barang = $id_barang;
    }

    public function getid_barang() {
        return $this->id_barang;
    }

    public function seturl_gambar_asli($url_gambar_asli) {
        $this->url_gambar_asli = $url_gambar_asli;
    }

    public function geturl_gambar_asli() {
        return $this->url_gambar_asli;
    }

    public function seturl_gambar_tumbnail($url_gambar_tumbnail) {
        $this->url_gambar_tumbnail = $url_gambar_tumbnail;
    }

    public function geturl_gambar_tumbnail() {
        return $this->url_gambar_tumbnail;
    }


    public function insertgambar() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $query = mysql_query("insert into gambar values('" . mysql_real_escape_string($this->getid_gambar()) . "','"
                . mysql_real_escape_string($this->geturl_gambar_asli()) . "','"
                . mysql_real_escape_string($this->geturl_gambar_tumbnail()) . "','"
                . mysql_real_escape_string($this->getid_barang()) . "')");
        if (!$query) {
            return 'error : ' . mysql_error();
        } else {
            return 'sukses';
        }
    }

    function gantiUkuran($fileAsal) {
        $thumb = new Imagick();
        $gbr=$fileAsal;
        $thumb->readimage($fileAsal);
        $thumb->resizeimage(200, 170, Imagick::FILTER_LANCZOS, 1);
        $thumb->writeimage($gbr);
        file_put_contents($gbr, file_get_contents("imageBarang/thumb"));
//        move_uploaded_file($_FILES[$thumb], "imageBarang/thumb/" . $thumb);
        $thumb->clear();
        $thumb->destroy();
//        $maxLebar = 200;
//        $maxTinggi = 170;
//        $img;
//        if ($format == "jpg") {
//            $img = imagecreatefromjpeg($fileAsal);
//        } else if ($format == "png") {
//            $img = imagecreatefrompng($fileAsal);
//        } else if ($format == "gif") {
//            $img = imagecreatefromgif($fileAsal);
//        }
//        $lebar = imagesx($img);
//        $tinggi = imagesy($img);
//
//        if ($tinggi > $maxTinggi) {
//            $newLebar = ($maxTinggi / $tinggi) * $lebar;
////            $newTinggi = $maxTinggi;
//            $newTinggi = $maxTinggi;
//        } if ($lebar > $maxLebar) {
//            $newTinggi = ($maxLebar / $lebar) * $tinggi;
//            $newLebar = $maxLebar;
////            $newTinggi = $tinggi * $ratio;
//        }
//        $newImg = imagecreatetruecolor($newLebar, $newTinggi);
//        $palsize = imagecolorstotal($img);
//        for ($index = 0; $index < $palsize; $index++) {
//            $colors = imagecolorsforindex($newImg, $index);
//            imagecolorallocate($newImg, $colors['red'], $colors['green'], $colors['blue']);
//        }
//        if ($format == "jpg") {
//            imagejpeg($newImg, "imageBarang/thumb/" . getid_gambar() . "_s." . $format);
//        } else if ($format == "png") {
//            imagepng($newImg, "imageBarang/thumb/" . getid_gambar() . "_s." . $format);
//        } else if ($format == "gif") {
//            imagegif($newImg, "imageBarang/thumb/" . getid_ambar() . "_s." . $format);
//        }
//        imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newLebar, $newTinggi, $lebar, $tinggi);
//        return $newImg;
    }

    function resizeNew($file) {
        $persen;
    }

}
